/**
 *  列表页
 *
 * 作者 wanghw
 * 日期 2024-09-18 14:27:18
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-hazardReportAreaUser'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#hazardReportAreaUser-table-form'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'hazardReportAreaUserTable',
            url: ctx + 'hazardReportAreaUser/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'checkbox'},
                {field: 'reportAreaUserId', title: '主键', minWidth: 120},
                {field: 'tenantId', title: '公司Id', minWidth: 120},
                {field: 'reportId', title: '隐患报告Id', minWidth: 120},
                {field: 'areaLeaderId', title: '班次负责人Id', minWidth: 120},
                {field: 'areaLeaderName', title: '班次负责人', minWidth: 120},
                {field: 'areaLeaderNo', title: '班次负责人工号', minWidth: 120},
            ]],
            toolbar: '#hazardReportAreaUser-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(hazardReportAreaUserTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(hazardReportAreaUserTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增', 'hazardReportAreaUser/add', {
                btn: ['提交', '取消'],
                area: [$(window).width() <= 750 ? '95%':'50','800px'],
                yes: function (index, layero) {
                    $('#hazardReportAreaUser-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                }
            });
        }
    });

    table.on('tool(hazardReportAreaUserTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;
        if (layEvent === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.reportAreaUserId);
            });
        }
        if (layEvent === 'edit') {
            febs.modal.open('修改', 'hazardReportAreaUser/update/' + data.reportAreaUserId, {
                area: [$(window).width() <= 750 ? '95%':'50','800px'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#hazardReportAreaUser-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });
        }
    });

    $delete.on('click', function () {
        var checkStatus = table.checkStatus('hazardReportAreaUserTable');
        if (!checkStatus.data.length) {
            febs.alert.warn('没有选择要删除的数据！');
        } else {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                var ids = [];
                layui.each(checkStatus.data, function (key, item) {
                    ids.push(item.reportAreaUserId);
                });
                deleteIds(ids.join(','));
            });
        }
    });

    function getQueryParams() {
        return {
            name: $searchForm.find('input[name="name"]').val()?.trim(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'hazardReportAreaUser/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});